package tcp;

import com.serotonin.io.serial.SerialParameters;
import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.ip.IpParameters;
import com.serotonin.modbus4j.msg.ReadExceptionStatusRequest;
import com.serotonin.modbus4j.msg.ReadExceptionStatusResponse;

public class treExceptionStatusTest {
	public static String ipAdress;
	public static int ipPort;
	public static int slaveId;
	public treExceptionStatusTest(String ipAdress1, int ipPort1, int slaveId1)
	{
		ipAdress = ipAdress1; 
		ipPort = ipPort1;
		 slaveId = slaveId1;
	};
	public static void remain1(){
		IpParameters ipParameters = new IpParameters();
        ipParameters.setHost(ipAdress);
        ipParameters.setPort(ipPort);
		ModbusFactory modbusFactory = new ModbusFactory();

		ModbusMaster master = modbusFactory.createTcpMaster(ipParameters, false);
        try {
			master.init();
		} catch (ModbusInitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        readExceptionStatusTest(master, slaveId);
	};
	
    public static void readExceptionStatusTest(ModbusMaster master, int slaveId3) {
        try {
            ReadExceptionStatusRequest request = new ReadExceptionStatusRequest(slaveId3);
            ReadExceptionStatusResponse response = (ReadExceptionStatusResponse) master.send(request);

            if (response.isException())
                System.out.println("Exception response: message=" + response.getExceptionMessage());
            else
                System.out.println(response.getExceptionStatus());
        }
        catch (ModbusTransportException e) {
            e.printStackTrace();
        }
    }
}
